﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.IO;

namespace HttpMultiThreadDownloadExample
{
    public partial class Form1 : Form
    {
        private const int threadNumber = 5;

        public Form1()
        {
            InitializeComponent();
        }

        private void button_download_Click(object sender, EventArgs e)
        {
            HttpDownloadFile(textBox_url.Text, textBox_name.Text);
        }

        private void HttpDownloadFile(string url, string tatgetName)
        {
            if (!IsWebResouceAvailable(url))
            {
                MessageBox.Show("指定资源无效");
                return;
            }
            listBox.Items.Add("同时进行线程：" + threadNumber);

            HttpWebRequest request;
            long filesieze = 0;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = WebRequestMethods.Http.Head;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                filesieze = response.ContentLength;
                listBox.Items.Add("文件大小：" + Math.Ceiling(filesieze/1024.0f));
                response.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            int downfileSize = (int)(filesieze / threadNumber);
            HttpDownload[] d = new HttpDownload[threadNumber];
            for (int i = 0 ; i<threadNumber;++i)
            {
                d[i] = new HttpDownload(listBox,i);
                if (i<threadNumber-1)
                {
                    d[i].FileSize = downfileSize;
                }
                else
                {
                    d[i].FileSize = (int)(filesieze - (i-1)*downfileSize);
                }
                d[i].IsFinish = false;
                d[i].Url = textBox_url.Text;
                d[i].TaretFileName = Path.GetFileNameWithoutExtension(textBox_name.Text)+".$$"+i;
            }

            Thread[] threads = new Thread[threadNumber];
            for (int i = 0 ; i<threadNumber ; ++i)
            {
                threads[i] = new Thread(d[i].Receive);
                threads[i].Start();
            }

            CombineFiles c = new CombineFiles(d, listBox, textBox_name.Text);
            Thread t = new Thread(c.Combine);
            t.Start();

        }

        private bool IsWebResouceAvailable(string url)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = WebRequestMethods.Http.Head;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return (response.StatusCode == HttpStatusCode.OK);
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
            	return false;
            }
        }
    }
}

